package org.bw.bookstore.book.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

public class EwmUtils {
	
	public static void createEwm(String url,  //网址,注意这里必须是一个公网的信息
	String parentPath,   //二维码生成之后保存的路径
	String imgName,      //二维码图片名称
	String imgSuffix) {  //图片的后缀   
		
        int width = 300; 
        int height = 300; 
        //二维码的图片格式 
        String format = "jpg"; 
        Hashtable hints = new Hashtable(); 
        //内容所使用编码 
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = null;
		try {
			bitMatrix = new MultiFormatWriter().encode(url,
			        BarcodeFormat.QR_CODE, width, height, hints);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        //生成二维码 
		String path = parentPath+File.separator+imgName+imgSuffix;
        File outputFile = new File(path); 
        try {
			MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
			System.out.println("图片应将生成，请前往"+path+"查看...");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		System.out.println("success...");
	}

}
